#!/usr/bin/env python3
# --*-- coding:UTF8 --*--

# import urllib.request
#
# #网址
# url = "http://www.jokeji.cn/list.htm"
#
# #请求
# request = urllib.request.Request(url)
# data = urllib.request.urlopen(request).read()
# data = data.decode('gbk')
#
# #打印结果
# print(data)
#
# #打印爬取网页的各类信息
#
# # print(type(response))
# # print(response.geturl())
# # print(response.info())
# print(response.getcode())


import requests         # 导入requests库
import re               # 导入正则表达式库
import urllib
import os

jokePage = requests.get('http://www.jokeji.cn/list.htm')
jokePage.encoding = 'gbk'
jokeList = re.findall('/jokehtml/[\w]+/[0-9]+.htm',jokePage.text)   # 使用正则表达式找到所有笑话页面的链接
jokeContent = requests.get('http://www.jokeji.cn/'+jokeList[0])     # 访问第一个链接
jokeContent.encoding = 'gbk'
jokes = re.findall('<P>[0-9].*</P>', jokeContent.text)      # 利用正则找到页面中的所有笑话
jokesall = "".join(jokes).replace("<p>","\n\n").replace("<BR>","\n").replace("<P>","\n").replace("</P>","")

#print(jokes)
print(jokesall)

# url = "https://oapi.dingtalk.com/robot/send?access_token=4861d92ae7c9356ea5896bfdf10293de0897eb0385b1bdfbe2fba593acfa06ff"
#
#
# #data = print('{"msgtype": "text", "text":{"content":"%s"}' % jokesall})
# data = {"msgtype": "text", "text":{"content":jokesall}}
#
# header = '"Content-Type: application/json"'
#
# command_all = "curl %s -H %s -d '%s'" % (url, header, data)
#
# print(data)
# print(type(jokesall))
# print(type(data))
# print(command_all)
# #os.system(command_all)
